<?php


namespace app\merchant\controller;


use app\common\controller\Common;
use app\common\model\Cards;
use app\common\model\Member;
use app\common\model\Merchant;
use app\common\model\VerfRecord;
use think\Db;

class Verification extends Common
{

    public function scan()
    {
        $vcode = request()->param('vcode');
        $openid= request()->param('openid');
        if (is_null($vcode) || strlen($vcode) < 20) return $this->errJson('非法请求v', ['tel' => '04715822568']);
        if (is_null($openid) || $openid=="") return $this->errJson('非法请求o', ['tel' => '04715822568']);
            $reMer = Merchant::where('openid','=',$openid)->find();
            if (empty($reMer)) return $this->errJson('数据异常');
        $merid = $reMer->merid;
        $str = substr($vcode, 5);
        $code = base64_decode($str);
        if (strlen($code) < 15) return $this->errJson('二维码非法', ['tel' => '04715822568']);
        $time = substr($code, -10);
        if (time() - $time > 60) return $this->errJson('二维码超时', ['tel' => '04715822568']);
        $cardid = substr($code, 0, strpos($code, $sai = config('app.sai')));
        $memid = substr($code, strpos($code, '|') + 1, strpos($code, '*') - strpos($code, '|') - 1);
        $retCards = Cards::where(['cardid' => $cardid])->find();
        if (empty($retCards)) return $this->errJson('数据异常', ['tel' => '04715822568']);
        if ($retCards->status == 2) {
            $endtime = date_create(date("Y-m-d", strtotime("+" . $retCards->term . " month", $retCards->use_time)));
            $retime = date_create();
            $T = date_diff($retime, $endtime);
            $num = $T->format('%R%a');
            if (intval($num) < 0) return $this->errJson('会员卡已过期', ['tel' => '04715822568']);

            $reMem = Member::where('memid', '=', $memid)->find();
            $retMer = Merchant::where('merid', '=', $merid)->with('category')->find();

            if (empty($reMem) || empty($retMer)) return $this->errJson('数据异常', ['tel' => '04715822568']);
            $retMer->verf_num++;
            $verfR = new VerfRecord();
            $verfR->memid = $memid;
            $verfR->merid = $merid;
            $verfR->mem_name = $reMem->name;
            $verfR->mer_name = $retMer->mername;
            $verfR->cate_id = $retMer->cate_id;
            $verfR->cate_name = $retMer->title;
            $verfR->area_id = $retMer->area_id;
            $verfR->mer_address = $retMer->address;
            $verfR->discount = $retMer->discount;
            $verfR->mer_img = $retMer->title_img;
            $verfR->time = time();
            $verfR->card_no = $retCards->card_no;
            $verfR->save();
            $retMer->save();
            $time = date('Y-m-d H:i', time());
            return $this->okJson('核销成功', ['distcount' => $retMer->discount, 'time' => $time, 'card_no' => $retCards->card_no]);
        } else {
            return $this->errJson('会员卡状态异常', ['tel' => '04715822568']);
        }

    }

    public function subVerification()
    {

    }

    public function merberverf()
    {
        $openid = request()->param('openid');
        if ($openid == "" || $openid == null) return $this->errJson('缺少参数');
        $reMer = Merchant::where('openid', '=', $openid)->field('merid')->find();
        if (empty($reMer)) return $this->errJson('数据异常');
        $merid = $reMer->merid;
        $page = $this->getPage();
        $pagesize = $this->getPageSize();
        $min = ($page - 1) * $pagesize;
        $max = $pagesize;
        $count = VerfRecord::where('merid', '=', $merid)->count();
        $verf = Db::query("SELECT memid,mem_name,card_no,area_id,cate_id,cate_name,discount,FROM_UNIXTIME(time,'%Y-%m-%d %H:%i') as time FROM verf_record WHERE merid=" . $merid . " limit " . $min . "," . $max);
        if (count($verf) == 0) return $this->errJson('暂无返回记录');
        return $this->listJson($verf, $count);
    }


}